// src/rtcService.js
import SDK from 'nim-web-sdk';

class RTCService {
  constructor() {
    this.nim = null;
    this.channelName = '';
  }

  init(appKey, account, token, onConnect, onError) {
    this.nim = SDK.NIM.getInstance({
      appKey,
      account,
      token,
      onconnect: onConnect,
      onerror: onError,
      // 其他回调函数，根据需要添加
    });
  }

  joinChannel(channelName, onSuccess, onFailure) {
    this.channelName = channelName;
    this.nim.joinChannel({
      channelName,
      done: (error, obj) => {
        if (error) {
          onFailure(error);
        } else {
          onSuccess(obj);
        }
      }
    });
  }

  leaveChannel(onSuccess, onFailure) {
    if (this.nim && this.channelName) {
      this.nim.leaveChannel({
        channelName: this.channelName,
        done: (error, obj) => {
          if (error) {
            onFailure(error);
          } else {
            onSuccess(obj);
          }
        }
      });
    }
  }

  destroy() {
    if (this.nim) {
      this.nim.destroy({
        done: () => {
          console.log('NIM instance destroyed');
        }
      });
    }
  }
}

export default new RTCService();